package logic

import (
	"cart_srv/cart"
	"context"
	"fmt"
	"strconv"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type CartCountLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCartCountLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CartCountLogic {
	return &CartCountLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CartCountLogic) CartCount(req *types.CartCountRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	userId := l.ctx.Value("userId").(string)
	uid, _ := strconv.Atoi(userId)
	fmt.Println(uid)
	checkedGoods, err := l.svcCtx.CartSrv.GetCartCheckedGoods(l.ctx, &cart.GetCartCheckedGoodRequest{UserId: int64(uid)})
	if err != nil {
		return &types.Response{
			Code:    -1,
			Message: "内部服务器错误",
			Data:    nil,
		}, nil
	}
	return &types.Response{
		Code:    0,
		Message: "获取统计数据 ok",
		Data: map[string]interface{}{
			"totalNum":   checkedGoods.GoodNum,
			"totalPrice": checkedGoods.GoodPrice,
		},
	}, nil
}
